package com.kanasco.vision.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import bb.access.po.CustomerVendorAddressInfo;
import bb.access.po.Part;
import bb.access.po.SalesOrder;

import com.kanasco.vision.service.ISales;

public class SaveSO extends SimpleFormController{
	ISales salesServices;
	
		public ISales getSalesServices() {
		return salesServices;
	}
		public void setSalesServices(ISales salesServices) {
		this.salesServices = salesServices;
	}
		public SaveSO() 
	{
		/*setCommandClass(Sales.class);
		setCommandName("sales");*/
			setCommandClass(SalesOrder.class);
			setCommandName("sales");
	}

	protected ModelAndView onSubmit(HttpServletRequest req,HttpServletResponse res,Object command,BindException exception) throws SQLException
  	{
        //Sales sale=(Sales)command;
		HttpSession session=req.getSession();
		SalesOrder sale=(SalesOrder) command;
        SalesOrder so=new SalesOrder();
        CustomerVendorAddressInfo cva=new CustomerVendorAddressInfo();
        so.setCustomerId(cva.getId());
        so.setSalesOrderNumber("S1400011");
        so.setCustomerId(sale.getCustomerId());
        so.setBillToId(sale.getBillToId());
        so.setShipToId(sale.getShipToId());
        so.setCustomerPo(sale.getCustomerPo());
        so.setContact(sale.getContact());
        so.setLastUser("ADMIN");
        so.setOrigin("ADMIN");
        so.setSalesman("Admin");
        //String cId=so.getCustomerAddressInfo().getId();
        String cuno=sale.getCustomerId();
        System.out.println("cid number from save is"+cuno);
        CustomerVendorAddressInfo custinfo=salesServices.load(cuno);
        //List<String> custinfo=salesServices.getCustInfo(cuno);
    
       /* System.out.println("Customer id from list is"+custinfo.get(0));
        */
       System.out.println("Customer id from list is"+ custinfo.getId());
       
        String custId=custinfo.getId();
        String custname=custinfo.getName();
        System.out.println("customer id issss"+custId);
        System.out.println("customer id issss"+custname);
        if(custId.equalsIgnoreCase(cuno))
        {
        	so.setCustomerAddressInfo(custinfo);
        	System.out.println("The number Exist in DB");
        	 salesServices.create(so);
        
        }
        
        else
        {
        	 System.out.println("The number Does Not Exist in DB");
        }
        //salesServices.create(so);
	    String view=getSuccessView();
	    System.out.println("view is"+view);
	    String salesOrderNumber=so.getSalesOrderNumber();
        System.out.println("number from save is"+salesOrderNumber);
        System.out.println("Number From Save is"+sale.getSalesOrderNumber());
        System.out.println("Contact From Save is"+so.getContact());
        List<String> soStatus=new ArrayList<String>();
		soStatus.add("NOT REVIEWED");
		soStatus.add("CANCELLED");
		List<String> language=new ArrayList<String>();
		language.add("English");
		language.add("Simplified Chinese");
		language.add("Traditional Chinese");
		List<String> incoTerm=salesServices.getIncoTerm();
		List<String> saleCode=salesServices.getSalesCode();
		List<String> currency=salesServices.getCurrency();
		List<String> projectList=salesServices.getProjectList();
		List<String> AcctList=salesServices.getAcctNumList();
		List<Integer> termList=salesServices.getTermsList();
		List<String> item_typlist=salesServices.getItemType();
		
	    List<String> item_uom=salesServices.getItemUOM();
		ModelAndView modelAndView=new ModelAndView(view,"sales",so);
	/*	ModelAndView modelAndView=new ModelAndView("CheckSO","sales",so);*/
		modelAndView.addObject("soStatus", soStatus);
		modelAndView.addObject("incoTerm",incoTerm);
		modelAndView.addObject("saleCode", saleCode);
		modelAndView.addObject("currency", currency);
		modelAndView.addObject("language",language);
		modelAndView.addObject("projectList", projectList);
		modelAndView.addObject("AcctList",AcctList);
		modelAndView.addObject("termList",termList);
		//modelAndView.addObject("custinfo",custinfo);
		session.setAttribute("custinfo", custinfo);
		/*Part item=new Part();
		modelAndView.addObject("item", item);*/

     	return modelAndView;
	
	  }

}
